<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Passwords</title>
<SCRIPT src="javascripts/pidcrypt.js"></SCRIPT>
<SCRIPT src="javascripts/pidcrypt_util.js"></SCRIPT>
<SCRIPT src="javascripts/asn1.js"></SCRIPT>
<!--needed for parsing decrypted rsa certificate-->
<SCRIPT src="javascripts/jsbn.js"></SCRIPT>
<!--needed for rsa math operations-->
<SCRIPT src="javascripts/rng.js"></SCRIPT>
<!--needed for rsa key generation-->
<SCRIPT src="javascripts/prng4.js"></SCRIPT>
<!--needed for rsa key generation-->
<SCRIPT src="javascripts/rsa.js"></SCRIPT>

<script src="scripts/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="scripts/json2.js" type="text/javascript"></script>
<script src="scripts/json.htmTable.js" type="text/javascript"></script>
<script type="text/javascript" src="jquery.qrcode.min.js"></script>


<!--needed for rsa en-/decryption-->

<link href="styles/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
	
	function deletePassword(id) {
		if (confirm("Are you sure?")) {
			$.ajax({
				url : '../passwords/delete/' + id,
				success : function(data) {
					alert('Entry deleted');
					$('#content').load('passwords.html');
				}
			});
		}
	}
	function showDecryptForm(pwhash) {
		$("#decrypt").show();
		$('#pwhash').text(pwhash);
	}

	function showCipher(value) {
		$('#cipher').text(value);
	}

	function showQR(value) {

		clearQR();
		var qr = $('#qrcode');
		jQuery('#qrcode').qrcode(value);
		window.setTimeout("clearQR()", 30 * 1000);
	}
	function clearQR() {
		$('#qrcode').empty();
	}
	function editPassword(id, pwhash) {
		$('#pwhash').text(pwhash);
		$("#decrypt").show();
		$('#pwhash').text(pwhash);
		$('#decryptBtn').click(
			function() {
				decrypt();
				var decryptedStr = $('#decrypted').val();
				$("#decrypt").hide();
				$('#edit').show();
				$('#password2Hash').val(decryptedStr);
				edit(id);
			}		
		);
		//edit(id);
	}
	$(document)
			.ready(
					function() {
						$('#edit').load('createPassword.html');
						$('#decrypt').load('decrypt.html');
						$('#edit').hide();
						$('#decrypt').hide();
				
						$
								.ajax({
									url : '../passwords/read',
									success : function(data) {
										var objectData = jQuery.parseJSON(data);
										var transformedData = new Array();
										$
												.each(
														data,
														function(index, value) {
															var ops = "<a href='#' onclick=javascript:showQR('"
																	+ value.passwordUserHashs[0].hash
																			.replace(
																					/\n/g,
																					'')
																	+ "')>Decrypt with QR</a> <a href='#' onclick=javascript:showDecryptForm('"
																	+ value.passwordUserHashs[0].hash
																			.replace(
																					/\n/g,
																					'')
																	+ "')>Decrypt in Browser</a> <a href='decrypt://"
																	+ value.passwordUserHashs[0].hash
																			.replace(
																					/\n/g,
																					'')
																	+ "' onclick=javascript:showCipher('"
																	+ value.passwordUserHashs[0].hash
																			.replace(
																					/\n/g,
																					'')
																	+ "')>Show Cipher</a> <a href='#' onclick=javascript:deletePassword('"
																	+ value.id
																	+ "')>Delete</a> <a href='#' onclick=javascript:editPassword('"
																	+ value.id + "'," + "'" + value.passwordUserHashs[0].hash
																	.replace(
																			/\n/g,
																			'')
																	+ "')>Edit</a>";
															transformedData[index] = {
																id : value.id,
																name : value.title,
																notes : value.notes,
																category : value.passwordCategory.passwordCategoryName,
																operations : ops
															}
														});
										$('#DynamicGrid').append(
												CreateTableView(
														transformedData,
														"lightPro", true))
												.fadeIn();
									}
								});

						$('#DynamicGridLoading').hide();
					});
</script>

</head>
<body>
	<h1>Passwords</h1>
	<form id="form1">
		<div id="DynamicGrid">
			<div id="DynamicGridLoading">
				<img src="images/loading.gif" /><span> Loading Data... </span>
			</div>
		</div>
	</form>

	<textarea id="cipher"></textarea>


	<div id="decrypt">
		
	</div>

	<div id="qrcode"></div>
	<div id="edit"></div>
</body>
</html>
